package com.keur.money.ui

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.Gravity
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.keur.money.R
import com.keur.money.databinding.ActMullaPaPoyBinding
import com.keur.money.http.HttpHelper
import com.keur.money.mvp.ProAct
import com.keur.money.utils.event.KeurEventType
import com.keur.money.utils.helper.KeurCustomConfigsHelper

class KeurPaPoyAct : ProAct() {

    private lateinit var binding: ActMullaPaPoyBinding
    private var linkTitle = ""

    companion object {
        private const val INTENT_LINK_TITLE = "linkTitle"
        private const val INTENT_LINK_URL = "linkUrl"
        fun start(context: Context, title: String, linkUrl: String) {
            val intent = Intent(context, KeurPaPoyAct::class.java)
            intent.putExtra(INTENT_LINK_TITLE,title)
            intent.putExtra(INTENT_LINK_URL, linkUrl)
            context.startActivity(intent)
        }
    }

    override fun getMullaRootView(): View {
        binding = ActMullaPaPoyBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initMullaView() {
        setMullaTitleBarVisibility(View.VISIBLE)
        setMullaTextTitle(getLinkTitle())
        setMullaTextTitleGravity(Gravity.CENTER)
        setMullaRightBtnRes(R.mipmap.mulla_kefu_icon)

        initWebViewSettings()
    }

    override fun initMullaListener() {
        //联系客服
        rootViewBinding.ivFun.setOnClickListener {
            showMullaKefuDialog("$linkTitle Page")
            HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_CUSTOMER,"$linkTitle Page")
        }
    }

    override fun initMullaData() {
        //牌照信息
        val licenseInfo = KeurCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.visibility = View.GONE
        } else {
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.addView(licenseView())
            binding.licenseLayout.visibility = View.VISIBLE
        }

        binding.ppWebView.loadUrl(getLinkUrl())
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.ppWebView.setInitialScale(100)
        binding.ppWebView.webChromeClient = mWebChromeClient
        binding.ppWebView.webViewClient = mWebClient
        val webSettings = binding.ppWebView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.progressBar.progress = newProgress
            if (newProgress == 100) {
                binding.progressBar.visibility = View.GONE
            } else {
                binding.progressBar.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?, ): Boolean {
            return shouldOverrideUrlLoading(view, request?.url.toString())
        }

        @Deprecated("Deprecated in Java", ReplaceWith("false"))
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    fun getLinkTitle(): String {
        return intent.extras?.getString(INTENT_LINK_TITLE).toString()
    }

    fun getLinkUrl(): String {
        return intent.extras?.getString(INTENT_LINK_URL).toString()
    }

    override fun onStart() {
        super.onStart()
        linkTitle = getLinkTitle()
        HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_WEBVIEW_LINK_START,"$linkTitle Page")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_WEBVIEW_LINK_END,"$linkTitle Page")
    }
}